package com.security.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录成功后的处理
 *
 * @author 顾志杰
 * @date 2020/7/7-14:19
 */
@Component
public class MySuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    private final static ObjectMapper OBJECTMAPPER = new ObjectMapper();

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request,
                                        HttpServletResponse response,
                                        Authentication authentication) throws ServletException, IOException {
        /*响应数据类型*/
        response.setContentType("application/json;charset=UTF-8");
        /*响应数据*/
        response.getWriter().write(OBJECTMAPPER.writeValueAsString("{'code':'200'}"));
    }
}
